package com.estar.assistsurvy.database;

import java.util.ArrayList;

import com.estar.assistsurvy.entity.Photo;

import android.content.Context;
import android.database.Cursor;

public class PhotoService {

	private myDBTools tool;
	
	public PhotoService(Context context) {
		tool=new myDBTools(context);
	}
	
	public void addPhoto(Photo photo){
		String addPhoto_sql="insert into photos(carNo ,claimTaskId ,"
			+"claimTaskType ,compCde  ,dptCde ,latitude ,longitude ,"
			+"picName ,picPath ,rptNo ,isUpdate ,imageTime ) values();";
		Object[] add_bindArgs={photo.getCarNo(),photo.getClaimTaskId(),
				photo.getClaimTaskType(),photo.getCompCde(),photo.getDptCde(),
				photo.getLatitude(),photo.getPhotoId(),photo.getPicName(),
				photo.getPicPath(),photo.getRptNo(),photo.getIsUpdate(),photo.getImageTime()};
		tool.writeDB(addPhoto_sql, add_bindArgs);
		tool.DBclose();
	}
	
	public void deletePhoto(Photo photo){
		String delPhotos_sql="delete from photos where photoId=?;";
		Object[] del_bindArgs={photo.getPhotoId()};
		tool.writeDB(delPhotos_sql, del_bindArgs);
		tool.DBclose();
	}
	
	public void updatePhoto(Photo photo){
		String updateAccounts_sql="update photos set isUpdate=? where photoId=?;";
		Object[] update_bindArgs={photo.getIsUpdate(),photo.getPhotoId()};
		tool.writeDB(updateAccounts_sql, update_bindArgs);
		tool.DBclose();
	}
	

	public ArrayList<Photo> selectAll(){
		ArrayList<Photo> list=new ArrayList<Photo>();
		String selectAll_sql="select * from photos;";
		Cursor cursor=tool.readDB(selectAll_sql, null);
		while(cursor.moveToNext()){
			Photo photo=new Photo();
			photo.setPhotoId(cursor.getInt(cursor.getColumnIndex("photoId")));
			photo.setCarNo(cursor.getString(cursor.getColumnIndex("carNo")));
			photo.setClaimTaskId(cursor.getString(cursor.getColumnIndex("claimTaskId")));
			photo.setClaimTaskType(cursor.getString(cursor.getColumnIndex("claimTaskType")));
			photo.setCompCde(cursor.getString(cursor.getColumnIndex("compCde")));
			photo.setDptCde(cursor.getString(cursor.getColumnIndex("dptCde")));
			photo.setLatitude(cursor.getString(cursor.getColumnIndex("latitude")));
			photo.setLongitude(cursor.getString(cursor.getColumnIndex("longitude")));
			photo.setPicName(cursor.getString(cursor.getColumnIndex("picName")));
			photo.setPicPath(cursor.getString(cursor.getColumnIndex("picPath")));
			photo.setRptNo(cursor.getString(cursor.getColumnIndex("rptNo")));
			photo.setIsUpdate(cursor.getString(cursor.getColumnIndex("isUpdate")));
			photo.setImageTime(cursor.getString(cursor.getColumnIndex("imageTime")));
			list.add(photo);
		}
		tool.DBclose();
		
		return list;
	}
	
	public ArrayList<Photo> selectRptNo(String rptNo){
		ArrayList<Photo> list=new ArrayList<Photo>();
		String selectRptNo_sql="select * from photos where rptNo=?;";
		String[] selectRptNo_bindArgs={rptNo};
		Cursor cursor=tool.readDB(selectRptNo_sql, selectRptNo_bindArgs);
		while(cursor.moveToNext()){
			Photo photo=new Photo();
			photo.setPhotoId(cursor.getInt(cursor.getColumnIndex("photoId")));
			photo.setCarNo(cursor.getString(cursor.getColumnIndex("carNo")));
			photo.setClaimTaskId(cursor.getString(cursor.getColumnIndex("claimTaskId")));
			photo.setClaimTaskType(cursor.getString(cursor.getColumnIndex("claimTaskType")));
			photo.setCompCde(cursor.getString(cursor.getColumnIndex("compCde")));
			photo.setDptCde(cursor.getString(cursor.getColumnIndex("dptCde")));
			photo.setLatitude(cursor.getString(cursor.getColumnIndex("latitude")));
			photo.setLongitude(cursor.getString(cursor.getColumnIndex("longitude")));
			photo.setPicName(cursor.getString(cursor.getColumnIndex("picName")));
			photo.setPicPath(cursor.getString(cursor.getColumnIndex("picPath")));
			photo.setRptNo(cursor.getString(cursor.getColumnIndex("rptNo")));
			photo.setIsUpdate(cursor.getString(cursor.getColumnIndex("isUpdate")));
			photo.setImageTime(cursor.getString(cursor.getColumnIndex("imageTime")));
			list.add(photo);
		}
		tool.DBclose();
		return list;
	}
}
